home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1381 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: char* to char array[] ?
  5. Date: 13 Jan 1996 00:18:59 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4d7fc3$i5g@umbc9.umbc.edu>
  8. References: <4d4n9s$9uv@ixnews2.ix.netcom.com> <1996Jan13.013852.19365@hns.com>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Ralph Sweitzer  <t_rsweitzer> wrote:
  13. |> Try something like this it should work.
  14. |> 
  15. |> /* prototype */
  16. |> char *called_finction(void);
  17.  
  18. That needs to be "char *called_function (void);". One point off for
  19. spelling ;-).
  20.  
  21. |> void calling_function(void)
  22. |> {
  23. |>   char *ptr;
  24. |>   ptr = called_function();
  25. |> }
  26. |> 
  27. |> char *called_function(void)
  28. |> {
  29. |>   char buf[] = {"hello world"};
  30. |>   return((char *)buf);
  31. |> }
  32.  
  33. Obviously you are trying to help whomever the original poster was, but
  34. advice like this will do more harm than good. See, C is a pass by value
  35. language except where arrays and function names decay to pointers. You
  36. are returning a local array which is no longer valid once the function
  37. called_function() exits and whatever value calling_function() receives
  38. is totally undefined.
  39.  
  40. See FAQ question 7.5...I won't post it so you'll have to actually
  41. read it yourself.
  42. -- 
  43. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  44.  
  45. Jonas J. Schlein  (schlein@gl.umbc.edu)
  46.